package com.langchain4j.simple.config;

import dev.langchain4j.model.chat.listener.ChatModelErrorContext;
import dev.langchain4j.model.chat.listener.ChatModelListener;
import dev.langchain4j.model.chat.listener.ChatModelRequestContext;
import dev.langchain4j.model.chat.listener.ChatModelResponseContext;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.chat.response.ChatResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName LangChainListener
 * @Description 监听
 * @createTime 13:52:00 2025/04/17
 */
@Slf4j
public class LangChainListener implements ChatModelListener {

    @Override
    public void onRequest(ChatModelRequestContext requestContext) {
        ChatRequest request = requestContext.chatRequest();
        Map<Object, Object> attributes = requestContext.attributes();
        // 记录请求
        log.info("request: {}", request);
    }

    @Override
    public void onResponse(ChatModelResponseContext responseContext) {
        ChatResponse response = responseContext.chatResponse();
        ChatRequest request = responseContext.chatRequest();
        Map<Object, Object> attributes = responseContext.attributes();
        // 记录响应
        log.info("response: {}", response);
    }

    @Override
    public void onError(ChatModelErrorContext errorContext) {
        Throwable error = errorContext.error();
        ChatRequest request = errorContext.chatRequest();
//        ChatModelResponse partialResponse = errorContext.partialResponse();
        Map<Object, Object> attributes = errorContext.attributes();
        // 记录错误
        log.info("error: {}", error);
    }
}
